package cn.zz.veggie.application.service;

import java.util.List;

import org.springframework.stereotype.Service;

import cn.zz.comm.core.types.query.PageQuery;
import cn.zz.comm.core.types.result.PageResult;
import cn.zz.comm.util.CollectorUtil;
import cn.zz.veggie.application.assembler.DelManAssembler;
import cn.zz.veggie.application.packer.DelManPacker;
import cn.zz.veggie.model.domain.DelMan;
import cn.zz.veggie.model.query.DelManQuery;
import cn.zz.veggie.persistence.repository.DelManRepository;
import cn.zz.veggie.web.types.dto.mgmt.user.MgmtDelManQueryDto;
import cn.zz.veggie.web.types.dto.mgmt.user.MgmtDelManSaveDto;
import cn.zz.veggie.web.types.vo.his.user.HisDelManVo;
import cn.zz.veggie.web.types.vo.mgmt.user.MgmtDelManVo;
import lombok.RequiredArgsConstructor;

/**
 * @author zhongq
 * @since 2024/7/14
 */
@Service
@RequiredArgsConstructor
public class DelManAppService {
	private final DelManRepository delManRepository;

	public List<HisDelManVo> mgmtSearch(String keyword) {
		final DelManQuery query = DelManQuery.ofKeyword(keyword);
		List<DelMan> delMans = delManRepository.findByPage(query, PageQuery.all());
		return CollectorUtil.bwl(delMans, DelManPacker::toHisDelManVo);
	}

	public PageResult<MgmtDelManVo> mgmtGetList(MgmtDelManQueryDto dto, PageQuery page) {
		DelManQuery query = DelManAssembler.buildMgmtQuery(dto);
		List<DelMan> delMans = delManRepository.findByPage(query, page);
		return PageResult.of(page, CollectorUtil.bwl(delMans, DelManPacker::toMgmtDelManVo));
	}

	public void mgmtSaveDelMan(MgmtDelManSaveDto dto) {
		DelMan delMan = DelManAssembler.buildSaveDomain(dto);
		delManRepository.save(delMan);
	}

}